/*
 * @Author       : mengqingxin
 * @Date         : 2020-10-20 22:40:55
 * @LastEditors  : mengqingxin
 * @LastEditTime : 2020-12-06 17:25:22
 */
const request = options => {
  let app = getApp()
  return new Promise((resolve, reject) => {
    const { data, method } = options
    if(data && method !== 'get') {
      options.data = JSON.stringify(data)
    }
    // 设置固定请求头
    let header = {
      'Content-Type': 'application/json'
    }
    // 请求头携带token
    if(app.globalData.token){
      header['sc-token'] = '' + app.globalData.token
    }
    wx.request({
      header: header,
      ...options,
      success: function(res) {
        if(res.data.success) {
          resolve(res.data)
        } else {
          //服务器返回错误提示
          // if (res.data.message.includes("token")) {
          //   getApp().wxLogin()
          //   wx.navigateTo({
          //     url:`/pages/index/index`
          //   })
          // }
          if(!res.data.message.includes('sessionKey')){
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 2000
            })
          }
          reject(res.data)
        }
        wx.hideLoading()
      },
      fail: function(res) {
        // 响应状态码错误提示
        wx.hideLoading()
        wx.showToast({
            title: "网络开小差了，请稍后重试",
            icon: 'none',
            duration: 1500
          })
        reject(res.data)
      }
    })
  })
}
export default request